package com.ymxx.jweb.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class MavenProjectGenerator {

	
	private String groupId = "com.yumuxiaoxiao";
	private String artifactId = null;
	private String packaging = "jar";
	private String version = "0.0.1-RELEASE";
	public MavenProjectGenerator(String artifactId){
		
		this.artifactId = artifactId;
	}
	public MavenProjectGenerator(String groupId,String artifactId,String packaging,String version){
		
		this(artifactId);
		this.groupId = groupId;
		this.packaging = packaging;
		this.version = version;
	}
	/**
	 * 
	 * @param dir
	 */
	private void isCleanDir(File dir){
		
		if(dir == null||!dir.exists()||!dir.isDirectory()){
			throw new RuntimeException("the parameter [dir] is illegal," +
					"maybe possiable null or not exists or not a really directory");
		}
		if(dir.list().length != 0){
			for(File f : dir.listFiles()){
				if(!f.getName().startsWith(".")){
					throw new RuntimeException("this dir "+dir.getAbsolutePath()+" is not clean, " +
							"please choose another dir or do some clean work. " +
							"if you do not know how to clean it. Oh,please tell me you are an idiot");
				}
			}
		}
	}
	/**
	 * 
	 * @param dirpath
	 */
	public void generateJavaProject(String dirpath){
		
		File projectBaseDir = new File(dirpath);
		isCleanDir(projectBaseDir);
		File mainDir = new File(dirpath+File.separator+"src"+File.separator+"main"+File.separator+"java");
		mainDir.mkdirs();
		File mainResDir = new File(dirpath+File.separator+"src"+File.separator+"main"+File.separator+"resources");
		mainResDir.mkdirs();
		File testDir = new File(dirpath+File.separator+"src"+File.separator+"test"+File.separator+"java");
		testDir.mkdirs();
		File testResDir = new File(dirpath+File.separator+"src"+File.separator+"test"+File.separator+"resources");
		testResDir.mkdirs();
		FileWriter fw = null;  
		try {
			String pomXML = 
					"<project xmlns=\"http://maven.apache.org/POM/4.0.0\"\n"+
					"\t\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"+
					"\t\txsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n"+
					"\t<modelVersion>4.0.0</modelVersion>\n"+
					"\t<groupId>"+groupId+"</groupId>\n"+
					"\t<artifactId>"+artifactId+"</artifactId>\n"+
					"\t<packaging>"+packaging+"</packaging>\n"+
					"\t<version>"+version+"</version>\n\n\n"+
					"</project>";
			fw = new FileWriter(new File(dirpath+File.separator+"pom.xml"));
			fw.write(pomXML);
			fw.close();
		} catch (IOException e) {
			
			e.printStackTrace();
		}finally{
			try {
				
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 
	 * @param dirpath
	 */
	public void generateJeeProject(String dirpath){
		generateJavaProject(dirpath);
		
		File webapp = new File(dirpath+File.separator+"src"+File.separator+"main"+File.separator+"webapp"+File.separator+"WEB-INF");
		webapp.mkdirs();
		File res = new File(dirpath+File.separator+"src"+File.separator+"main"+File.separator+"webapp"+File.separator+"resources");
		res.mkdirs();
	}
	public static void main(String[] args) {
		
		new MavenProjectGenerator("com.yumuxiaoxiao","ymxx-web","war","1.0.0-RELEASE")
			.generateJeeProject("E:/git@OsChina/ymxx-jweb/ymxx-web");
	}
}
